package com.seckill.service.impl;

import com.seckill.domain.Product;
import com.seckill.domain.SeckillActivity;
import com.seckill.mapper.ProductMapper;
import com.seckill.mapper.SeckillActivityMapper;
import com.seckill.service.SeckillActivityService;
import com.seckill.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.lang.reflect.Field;
import java.util.*;

@Service

public class SeckillActivityServiceImpl implements SeckillActivityService {
    @Autowired
    private SeckillActivityMapper seckillActivityMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public Long createSeckillActivity(SeckillActivity seckillActivity) {

        SeckillActivity seckillActivity1 = new SeckillActivity();
        seckillActivity1.setCreateTime(new Date());
        seckillActivity1.setStartTime(seckillActivity.getStartTime());
        seckillActivity1.setEndTime(seckillActivity.getEndTime());
        seckillActivity1.setTotalStock(seckillActivity1.getTotalStock());
        seckillActivity1.setAvailableStock(seckillActivity.getTotalStock());
        seckillActivity1.setSeckillLimit(1);
        seckillActivityMapper.insertSeckillActivity(seckillActivity1);
        return seckillActivity.getId();
    }

    @Override
    public R getAllActivities() {
        List<SeckillActivity> all = seckillActivityMapper.findAll();
        return R.OK(all);
    }

    @Override
    public R getProductsByActivity(Long activityId) {
        List<Map<String, Object>> result = new ArrayList<>();
        List<SeckillActivity> activities = seckillActivityMapper.findAll();
        for (SeckillActivity activity : activities) {
            Map<String, Object> map = new HashMap<>();
            map.put("activity", activity);
            if (activity.getId().equals(activityId)) {
                Product product = productMapper.selectById(activity.getProductId());
                if (product != null) {
                    // 动态设置库存信息
                    try {
                        Field availableStockField = product.getClass().getDeclaredField("availableStock");
                        availableStockField.setAccessible(true);
                        availableStockField.set(product, activity.getAvailableStock());
                        Field totalStockField = product.getClass().getDeclaredField("totalStock");
                        totalStockField.setAccessible(true);
                        totalStockField.set(product, activity.getTotalStock());
                    } catch (Exception e) {
                        // ignore
                        throw new RuntimeException(e);
                    }
                    map.put("product", product);
                    result.add(map);
                }
            }
        }
            return R.OK(result);
    }

    @Override
    public List<Map<String, Object>> getAllActivityProducts() {
        List<Map<String, Object>> result = new ArrayList<>();
        List<SeckillActivity> activities = seckillActivityMapper.findAll();
        for ( SeckillActivity activity : activities) {

            Map<String, Object> map = new HashMap<>();
            map.put("activity", activity);
            Product product = productMapper.selectById(activity.getProductId());
            map.put("product", product);
            result.add(map);
        }
        return result;
    }






} 